package com.mryang.hds.controller;

import com.auth0.jwt.JWT;
import com.mryang.hds.entity.PunchInfo;
import com.mryang.hds.entity.QuestionSolution;
import com.mryang.hds.mapper.UserMapper;
import com.mryang.hds.service.IPunchInfoService;
import com.mryang.hds.service.IUserService;
import com.mryang.hds.service.impl.UserServiceImpl;
import com.mryang.hds.utils.DateUtil;
import com.mryang.hds.utils.JWTUtil;
import com.mryang.hds.utils.PunchUtil;
import com.mryang.hds.utils.ResultJson;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author Genius
 * @version 1.0.0
 * @ClassName HealthInfoController.java
 * @Description TODO 健康信息控制器（健康打卡）
 * @createTime 2023年01月12日 15:55:00
 */

@RestController
@RequestMapping("/api/health")
public class HealthInfoController {

    @Resource
    private IPunchInfoService iPunchInfoService;

    @Resource
    private IUserService iUserService;

    /**
     * 打卡
     * @return
     */
    @PostMapping("/addPunch")
    public ResultJson insertHealthPunch(@RequestParam("healthPunchInfo") String healthPunchInfo,
                                        @RequestParam("userId") Integer userId,
                                        HttpServletRequest httpServletRequest){
        // 获取用户编号
        Integer uid = JWTUtil.getTokenInfoIndex(httpServletRequest.getHeader("token"));
        String temp = "";

        if (!uid.equals(userId) && userId != null){

            temp=iUserService.getUserById(uid).getUName()+"-"+ uid;
            uid = userId;
        }

        // 获取当前时间
        String time = DateUtil.dateToString("yyyy-MM-dd HH:mm:ss");

        // 处理字符串
        List<PunchInfo> punchInfoList = PunchUtil.getQSList(healthPunchInfo, uid, time);

        // 添加健康打卡（问题）信息
        iPunchInfoService.addPunchInfo(punchInfoList, uid, time, temp);


        return ResultJson.success("健康打卡成功！");
    }



    /**
     * 获取所有题目信息
     * @return
     */
    @GetMapping("/getHealthQuestion")
    public ResultJson getAllQuestion(){

        // List<QuestionSolution> allQuestion = iPunchInfoService.getAllQuestion();
        List<String> allQuestion = iPunchInfoService.getAllQuestion();

        // System.out.println(allQuestion.toString());

        return ResultJson.success(allQuestion);
    }

    /**
     * 获取所有题目及选项信息
     * @return
     */
    @GetMapping("/getQuestionSolution")
    public ResultJson getQuestionSolution(){

        Map<String, List<QuestionSolution>> questionSolution = iPunchInfoService.getQuestionSolution();

        // System.out.println(questionSolution.toString());

        return ResultJson.success(questionSolution);
    }


}
